package shape

import (
	"gitee.com/git_public/vfaith/sugar/reflectx"
	"log"
)

type MapGnc[T any] struct {
	store map[string]T
	keys  []string
}

func NewMapGnc[T any]() *MapGnc[T] {
	mg := new(MapGnc[T])
	mg.store = make(map[string]T)
	mg.keys = make([]string, 0)
	return mg
}

func (mg *MapGnc[T]) MapGncInit() *MapGnc[T] {
	mg.store = make(map[string]T)
	mg.keys = make([]string, 0)
	return mg
}

func (mg *MapGnc[T]) Get(key string) (T, bool) {
	value, exists := mg.store[key]
	return value, exists
}

func (mg *MapGnc[T]) Add(key string, val T) bool {
	if _, exists := mg.store[key]; exists {
		log.Printf("Map Add 【%s】已存在", key)
		return false
	}
	mg.keys = append(mg.keys, key)
	mg.store[key] = val
	return true
}

func (mg *MapGnc[T]) Value(key string) any {
	return mg.store[key]
}

func (mg *MapGnc[T]) RangeStore(f func(m map[string]T)) {
	f(mg.store)
}

func (mg *MapGnc[T]) RangeStoreMap(f func(k string, v T)) {
	for k, v := range mg.store {
		f(k, v)
	}
}

func (mg *MapGnc[T]) RangeStoreReflect(funcName string) {
	for k, v := range mg.store {
		log.Printf("初始化对象【%s】", k)
		reflectx.FuncExecByName(v, funcName)
	}
}

func main() {
	k := new(MapGnc[string]).MapGncInit()

	log.Print(k)
}
